Java

recursiveaction java并发线程

2015/01/12 01:45 5933 次阅读王梓
★ 打赏
✸ ✸ ✸
package com.lzw;

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;
import java.util.concurrent.TimeUnit;

/**
 * @作者 王梓
 * 莫失莫忘 仙寿恒昌
 * 2015年1月11日
 * Email:277215243@qq.com
 */
public class Test extends RecursiveAction {
      
public static void main(String args[]) throws InterruptedException{
      ForkJoinPool pool=new ForkJoinPool();
       pool.submit(new Test(0,30));
       pool.awaitTermination(2, TimeUnit.SECONDS);
       pool.shutdown();
}

/* (non-Javadoc)
 * @see java.util.concurrent.RecursiveAction#compute()
 */
private static final int threshold=5;
private int start ;
private int end ;
public Test(int start ,int end){
       this.start =start ;
       this.end =end ;
}


@Override
protected void compute() {
       // TODO Auto-generated method stub

       if(end -start <threshold){
             for(int i =start ;i <end;i++){
                  System. out.println(Thread.currentThread().getName()+ " i value:"+i);
            }
      } else{
             int middle =(start +end )/2;
            Test t1= new Test(start ,middle );
            Test t2= new Test(middle ,end );
            
             t1.fork();
             t2.fork();
      }
}
}

 

✸ ✸ ✸

📜 版权声明

本文作者:王梓 | 原文链接:https://www.bthlt.com/note/3286704-Javarecursiveaction java并发线程

出处:葫芦的运维日志 | 转载请注明出处并保留原文链接

📜 留言板

留言提交后需管理员审核通过才会显示